﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reticulum
{
    class DBHelps
    {
        public static string conn = "server=112.74.46.59;database=Netdisk;uid=sa;pwd=5201314Qq";
        public static SqlConnection SQL = null;
        public static SqlCommand command = null;
        public static SqlDataReader reader = null;

        public static void Connection()//链接数据库得方法
        {
            try
            {
                if (SQL == null)//判断数据库连接对象是否为空
                {
                    SQL = new SqlConnection(conn);
                }
                if (SQL.State == ConnectionState.Closed)//判断链接状态为关闭
                {
                    //打开数据库
                    SQL.Open();
                }
                if (SQL.State == ConnectionState.Broken)//判断链接是否在中途中断
                {
                    //重启数据库
                    SQL.Close();
                    SQL.Open();
                }
                else
                {
                    //数据库链接正常，无任何错误
                }
            }
            catch (Exception)
            {
                MessageBox.Show("访问服务器数据库失败");
                SQL.Close();
                throw;
            }
        }

        public static bool Command(string DML)//增,删,改,
        {
            //启用数据库
            Connection();

            //创建命令对象
            command = new SqlCommand(DML, SQL);

            //执行命令
            return command.ExecuteNonQuery() > 0;


        }
        
        public static SqlDataReader Select(string DML) //查,
        {
            //启用数据库
            Connection();

            //创建命令对象
            command = new SqlCommand(DML, SQL);


            if (reader==null)
            {
                 reader = command.ExecuteReader();
                return reader;
            }

           
           
            //创建DataReader对象

            //返回值
            return reader;
        }

        //DBHelps.reader = null;

        public static DataSet Adapter(string DML)
        {
            DataSet dataSet = new DataSet();

            SqlDataAdapter adapter = new SqlDataAdapter(DML, SQL);

            adapter.Fill(dataSet);

            return dataSet;
        }

    }
}
